package coop.tecso.modelo1.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class GeneratorId {

	private static GeneratorId instance;
	
	private static Map<String, Integer> idMap = new HashMap<String, Integer>();
	
	static {
		
		UtilDB db =  UtilDB.getUtilDB();
		
		Connection conn = null;
		
		try {
			
			conn = db.getConnection();
			
			Statement stm = conn.createStatement();
			
			ResultSet rs = stm.executeQuery("select * from Generator_ID");
			
			rs.next();
		 
			int columnCount = rs.getMetaData().getColumnCount();
			
			for (int i=1; i <= columnCount; i++){
				
				String columnName = rs.getMetaData().getColumnName(i).toLowerCase();
				Integer value = rs.getInt(i);
				idMap.put(columnName, value);
				
			}
			
							
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		} finally {
			try{
        		if(conn!=null){
        			conn.close();
        		}
        	} catch (SQLException e){
        		e.printStackTrace();
        	}		
				
		}
		
		
		
	}
	
	
	
	private GeneratorId(){
		
		
	}
	
    public static GeneratorId getGeneratorId()

    {

        if (instance == null)

        {

            instance = new GeneratorId();

        }

        return instance;

    }
	
    
	public Integer getId(String table){

		UtilDB db =  UtilDB.getUtilDB();
		
		Connection conn = null;
		
		try {
			
			conn = db.getConnection();
			
			Statement stm = conn.createStatement();
			
			ResultSet rs = stm.executeQuery("select * from Generator_ID");
			
			rs.next();
		 
			int columnCount = rs.getMetaData().getColumnCount();
			
			for (int i=1; i <= columnCount; i++){
				
				String columnName = rs.getMetaData().getColumnName(i).toLowerCase();
				Integer value = rs.getInt(i);
				idMap.put(columnName, value);
				
			}
			
			
		} catch (Exception e) {			
			e.printStackTrace();
			
		} finally {
			try{
        		if(conn!=null){
        			conn.close();
        		}
        	} catch (SQLException e){
        		e.printStackTrace();
        	}		
				
		}			
		
		return idMap.get(table.toLowerCase())+1;
		
	}
    
	/** getMaxId de la tabla y setMaxId en la tabla generator_ID. **/
	
	public void setId(String tabla, Integer maxId) throws Exception{
		
		tabla = tabla.toLowerCase();
		
		UtilDB db =  UtilDB.getUtilDB();
		
		Connection conn = null;
		
		try {
			
			conn = db.getConnection();
			
			Statement stm = conn.createStatement();
			
			ResultSet rs = stm.executeQuery("select max(id) id from "+tabla);
			
			rs.next();
			
			Integer value = rs.getInt("id");
			
			
			stm.executeUpdate("update generator_id set "+tabla+"="+value+" where "+tabla+"="+maxId);
				
				
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		} finally {
			try{
        		if(conn!=null){
        			conn.close();
        		}
        	} catch (SQLException e){
        		e.printStackTrace();
        		throw new Exception(e.getMessage()+" : "+e.getSQLState());
        	}		
				
		}

		
	}
 
	
}
